package gui;

import io.ItemBusqueda;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Cursor;
import java.awt.Image;
import java.rmi.RemoteException;
import java.util.List;
import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import logica.Nucleo;

/**
 *
 * @author Mariano Zapata
 */
public class DialogoBusquedaEstado extends javax.swing.JPanel {

    private Object retorno;
    private Nucleo nucleo;
    private DefaultListModel modeloLista;
    
    /** Creates new form DialogoBusqueda */
    public DialogoBusquedaEstado(Nucleo nucleo)
    {
        initComponents();
        Image icono= new ImageIcon(getClass().getResource("/gui/icono/logo_chico.png")).getImage();
        dialogo.setIconImage(icono);
        dialogo.getContentPane().setLayout(new BorderLayout());
        modeloLista= new DefaultListModel();
        RenderListaBusqueda render= new RenderListaBusqueda();
        lista.setModel(modeloLista);
        lista.setCellRenderer(render);
        retorno= null;
        this.nucleo= nucleo;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        dialogo = new javax.swing.JDialog();
        etiqueta = new javax.swing.JLabel();
        buscar = new javax.swing.JButton();
        barra = new javax.swing.JProgressBar();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        lista = new javax.swing.JList();
        jLabel2 = new javax.swing.JLabel();
        ruta = new javax.swing.JTextField();
        ver = new javax.swing.JButton();
        estado = new javax.swing.JComboBox();

        dialogo.setTitle("Buscar");
        dialogo.setModal(true);
        dialogo.setResizable(false);
        dialogo.addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                cruz(evt);
            }
        });

        javax.swing.GroupLayout dialogoLayout = new javax.swing.GroupLayout(dialogo.getContentPane());
        dialogo.getContentPane().setLayout(dialogoLayout);
        dialogoLayout.setHorizontalGroup(
            dialogoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        dialogoLayout.setVerticalGroup(
            dialogoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        etiqueta.setText("Estado del expediente a buscar:");

        buscar.setText("Buscar");
        buscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buscar(evt);
            }
        });

        barra.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        barra.setString("Buscando, por favor espere...");

        jLabel1.setText("Resultado de la búsqueda:");

        lista.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        lista.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                listaMouse(evt);
            }
        });
        lista.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                seleccionoLista(evt);
            }
        });
        lista.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                listaKey(evt);
            }
        });
        jScrollPane1.setViewportView(lista);

        jLabel2.setText("Ruta:");

        ruta.setEditable(false);

        ver.setText("Ver");
        ver.setToolTipText("Buscar el elemento seleccionado en el árbol de directorio.");
        ver.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ver(evt);
            }
        });

        estado.setMaximumRowCount(5);
        estado.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Iniciado", "Apertura de la prueba", "Alegatos", "Sentencia", "Apelado" }));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 439, Short.MAX_VALUE)
                    .addComponent(etiqueta, javax.swing.GroupLayout.DEFAULT_SIZE, 439, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(estado, 0, 368, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(buscar))
                    .addComponent(barra, javax.swing.GroupLayout.DEFAULT_SIZE, 439, Short.MAX_VALUE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 439, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 439, Short.MAX_VALUE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 439, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(ruta, javax.swing.GroupLayout.DEFAULT_SIZE, 384, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(ver)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(etiqueta)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(buscar)
                    .addComponent(estado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(barra, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(ruta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(ver))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents
    
    private void buscar(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buscar
        this.setCursor(new Cursor(Cursor.WAIT_CURSOR));
        this.prepararParaBusqueda();
        try {
            Integer i= new Integer(estado.getSelectedIndex());
            this.cargarLista(nucleo.buscarExpedienteEstado(i.byteValue()));
            this.detenerBarra();
            this.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
        } catch (RemoteException ex) {retorno= ex.getMessage();
                                      this.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
                                      dialogo.setVisible(false);}
    }//GEN-LAST:event_buscar

    private void seleccionoLista(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_seleccionoLista
        retorno= lista.getSelectedValue();
        if (retorno != null)
            ruta.setText(((ItemBusqueda) retorno).getRutaLegible());
        else
            ruta.setText("");
    }//GEN-LAST:event_seleccionoLista

    private void listaMouse(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_listaMouse
        if (evt.getButton() == java.awt.event.MouseEvent.BUTTON1 && evt.getClickCount() == 2)
            this.ver(null);
    }//GEN-LAST:event_listaMouse

    private void listaKey(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_listaKey
        if (evt.getKeyCode() == java.awt.event.KeyEvent.VK_ENTER)
            this.ver(null);
    }//GEN-LAST:event_listaKey

    private void ver(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ver
        if (retorno != null)
            dialogo.setVisible(false);
    }//GEN-LAST:event_ver

    private void cruz(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_cruz
        retorno= null;
        dialogo.setVisible(false);
    }//GEN-LAST:event_cruz

    //EL RETORNO ES NULO SI CERRÓ CON LA CRUZ, UN STRING SI HUBO ERROR O UN ITEMBUSQUEDA SI HAY QUE BUSCARLO EN EL ÁRBOL.
    public Object hacerVisible(Component c)
    {
        dialogo.getContentPane().add(this, BorderLayout.CENTER);
        dialogo.pack();
        dialogo.setLocationRelativeTo(c);
        dialogo.setVisible(true);
        dialogo.dispose();
        return retorno;
    }
    
    private void prepararParaBusqueda()
    {
        modeloLista.clear();
        ruta.setText("");
        retorno= null;
        barra.setStringPainted(true);
        barra.setIndeterminate(true);
    }
    
    private void detenerBarra()
    {
        barra.setStringPainted(false);
        barra.setIndeterminate(false);
    }
    
    private void cargarLista(List<ItemBusqueda> l)
    {
        for (ItemBusqueda ib : l)
        {
            ib.setTipoBusqueda(DialogoBusqueda.BUSCAR_EXPEDIENTE_ESTADO);
            modeloLista.addElement(ib);
        }
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JProgressBar barra;
    private javax.swing.JButton buscar;
    private javax.swing.JDialog dialogo;
    private javax.swing.JComboBox estado;
    private javax.swing.JLabel etiqueta;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JList lista;
    private javax.swing.JTextField ruta;
    private javax.swing.JButton ver;
    // End of variables declaration//GEN-END:variables
}
